﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import org.robotlegs.core.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.*;
    import net.wooga.pets.osx.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.controllers.startup.*;

    public class GameViewMediator extends Mediator implements IMediator {

        private const MIN_DISTANCE_TO_ALLOW_DRAGGING:int = 1;

		[Inject]
        public var gameView:GameView;
		[Inject]
        public var gameViewModel:GameViewModel;
		[Inject]
        public var tickModel:TickModel;
		[Inject]
        public var roomSlotsModel:RoomSlotsModel;
		[Inject]
        public var environmentModel:EnvironmentModel;
        private var _mouseStartPosition:Point;
        private var _stageCenter:Point;
        private var _targetSlot:Slot;
        private var mouseWheelEnabled:Boolean = true

        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, EventConstants.ZOOM_IN_ROOM, this.zoomInRoom);
            eventMap.mapListener(eventDispatcher, EventConstants.SCALE_CHANGED, this.scaleChanged);
            eventMap.mapListener(eventDispatcher, EventConstants.SET_TO_POSITION, this.setToPosition);
            eventMap.mapListener(eventDispatcher, EventConstants.SHOW_ROOM, this.showRoom);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_GAME_VIEW_NAVIGATION, this.enableGameViewNavigation);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_GAME_VIEW_NAVIGATION, this.disableGameViewNavigation);
            eventMap.mapListener(eventDispatcher, EventConstants.CENTER_GAMEVIEW, this.centerGameView);
            eventMap.mapListener(eventDispatcher, EventConstants.ADD_HIDDEN_BIRD, this.addHiddenBird);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_HIDDEN_BIRD, this.removeHiddenBird);
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_POPUP, this.onCreatePopup);
            this.gameView.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.stage_mouseDown);
            this.gameView.stage.addEventListener(MouseEvent.MOUSE_UP, this.stage_mouseUp);
            this.gameView.hospitalScale = this.gameViewModel.hospitalViewScale;
            this._stageCenter = new Point((this.gameView.stage.stageWidth / 2), ((this.gameView.stage.stageHeight / 2) - (GameView.MAIN_MENU_HEIGHT / 2)));
            this.gameViewModel.setGameScaleByIndex(GameViewModel.START_SCALE);
            var _local1 = "2";
            dispatch(new InfoEvent(EventConstants.SHOW_ROOM, _local1));
            MouseWheelEnabler.init(contextView.stage);
        }
        private function onCreatePopup(_arg1:InfoEvent):void{
            this.stopDragging();
        }
        private function stage_mouseWheel(_arg1:MouseEvent):void{
            var _local2:int;
            if (_arg1.delta){
                _local2 = ((_arg1.delta > 0)) ? 1 : -1;
                if (_local2 == 1){
                    this.gameViewModel.increaseScaleFactor(true);
                } else {
                    if (_local2 == -1){
                        this.gameViewModel.decreaseScaleFactor(true);
                    };
                };
            };
        }
        private function stage_mouseUp(_arg1:MouseEvent):void{
            this.stopDragging();
        }
        private function stopDragging():void{
            if (this.mouseWheelEnabled){
                this.enableMousewheel();
            };
            this.gameView.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMove);
            dispatch(new Event(EventConstants.UPDATE_RENDER_LIST));
            this.tickModel.pause = false;
        }
        private function stage_mouseDown(_arg1:MouseEvent):void{
            this.disableMousewheel();
            this._mouseStartPosition = new Point(this.gameView.stage.mouseX, this.gameView.stage.mouseY);
            this.gameView.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMove);
        }
        private function stage_mouseMove(_arg1:MouseEvent):void{
            if (this.isConditionValidForStartDragging()){
                this.gameView.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.stage_mouseMove);
                this.tickModel.pause = true;
                dispatch(new Event(EventConstants.UPDATE_RENDER_LIST_FOR_DRAGGING));
                this.gameView.startDragging();
            };
        }
        private function isConditionValidForStartDragging():Boolean{
            var _local1:Number = (this.gameView.stage.mouseX - this._mouseStartPosition.x);
            var _local2:Number = (this.gameView.stage.mouseY - this._mouseStartPosition.y);
            var _local3:Number = Math.sqrt(((_local1 * _local1) + (_local2 * _local2)));
            return ((((_local3 > this.MIN_DISTANCE_TO_ALLOW_DRAGGING)) && (!(this.gameView.isDragging))));
        }
        private function showRoom(_arg1:InfoEvent):void{
            this.gameView.calculateBorders();
            var _local2:int = int(_arg1.id);
            this._targetSlot = this.roomSlotsModel.getSlotById(_local2);
            this.centerTargetRoom();
            dispatch(new Event(EventConstants.UPDATE_RENDER_LIST));
        }
        private function centerGameView(_arg1:Event=null):void{
            var _local2:Number = (-((((this.gameView.background.width * this.gameViewModel.gameViewScale) - GameConstants.STAGE_WIDTH) / 2)) - this.gameView.getVectorDisplayOffset().x);
            log((" gameView.getVectorDisplayOffset().x = " + this.gameView.getVectorDisplayOffset().x));
            this.gameViewModel.gameViewPosition = new Point(_local2, this.gameView.topBorder);
        }
        private function enableGameViewNavigation(_arg1:Event=null):void{
            this.enableMousewheel();
            this.gameView.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.stage_mouseDown);
            this.gameView.stage.addEventListener(MouseEvent.MOUSE_UP, this.stage_mouseUp);
            this.mouseWheelEnabled = true;
        }
        private function disableGameViewNavigation(_arg1:Event=null):void{
            this.disableMousewheel();
            this.gameView.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.stage_mouseDown);
            this.gameView.stage.removeEventListener(MouseEvent.MOUSE_UP, this.stage_mouseUp);
            this.mouseWheelEnabled = false;
        }
        private function enableMousewheel():void{
            this.gameView.stage.addEventListener(MouseEvent.MOUSE_WHEEL, this.stage_mouseWheel);
        }
        private function disableMousewheel():void{
            this.gameView.stage.removeEventListener(MouseEvent.MOUSE_WHEEL, this.stage_mouseWheel);
        }
        private function setToPosition(_arg1:InfoEvent):void{
            var _local2:Point;
            if (_arg1.id == GameViewModel.ID){
                _local2 = _arg1.info.position;
                this.gameView.setPositionAndCheckBorders(_local2);
                dispatch(new Event(EventConstants.UPDATE_RENDER_LIST));
            };
        }
        private function scaleChanged(_arg1:Event):void{
            this.gameView.stage.quality = StageQuality.MEDIUM;
            var _local2:Number = -(this.gameView.x);
            var _local3:Number = -(this.gameView.y);
            if (this.gameViewModel.mouseWheel){
                _local2 = (_local2 + contextView.stage.mouseX);
                _local3 = (_local3 + contextView.stage.mouseY);
            } else {
                _local2 = (_local2 + this._stageCenter.x);
                _local3 = (_local3 + this._stageCenter.y);
            };
            this.gameView.scale(this.gameViewModel.gameViewScale, _local2, _local3);
            this.gameView.hospitalScale = this.gameViewModel.hospitalViewScale;
            this.gameView.stage.quality = StageQuality.HIGH;
            dispatch(new Event(EventConstants.UPDATE_RENDER_LIST));
        }
        private function zoomInRoom(_arg1:InfoEvent):void{
            var _local2:int = int(_arg1.id);
            this._targetSlot = this.roomSlotsModel.getSlotById(_local2);
            this.centerTargetRoom();
            this.performStepZoomIn();
        }
        private function performStepZoomIn():void{
            this.tickModel.pause = true;
            this.gameViewModel.increaseScaleFactor(false);
            var _local1:int = this.gameViewModel.getLeftNumberOfStepsToMax();
            var _local2:Timer = new Timer(150, _local1);
            _local2.addEventListener(TimerEvent.TIMER, this.stepTimer_timer);
            _local2.addEventListener(TimerEvent.TIMER_COMPLETE, this.stepTimer_timerComplete);
            _local2.start();
        }
        private function stepTimer_timerComplete(_arg1:TimerEvent):void{
            var _local2:Timer = Timer(_arg1.target);
            _local2.removeEventListener(TimerEvent.TIMER_COMPLETE, this.stepTimer_timerComplete);
            _local2.removeEventListener(TimerEvent.TIMER, this.stepTimer_timer);
            _local2 = null;
            this.tickModel.pause = false;
            dispatch(new Event(EventConstants.ZOOM_INTO_ROOM_COMPLETE));
        }
        private function stepTimer_timer(_arg1:TimerEvent):void{
            this.centerTargetRoom();
            this.gameViewModel.increaseScaleFactor(false);
        }
        private function centerTargetRoom():void{
            var _local1:Point = this._targetSlot.position.clone();
            _local1.x = ((_local1.x * this.gameViewModel.hospitalViewScale) + (GameConstants.HOSPITAL_CENTER_POSITION.x * this.gameViewModel.gameViewScale));
            _local1.y = ((_local1.y * this.gameViewModel.hospitalViewScale) + (GameConstants.HOSPITAL_CENTER_POSITION.y * this.gameViewModel.gameViewScale));
            var _local2:Point = new Point();
            _local2.x = (((-(_local1.x) + this._stageCenter.x) - ((GameConstants.ROOM_WIDTH * this.gameViewModel.hospitalViewScale) / 2)) - this.gameView.getVectorDisplayOffset().x);
            _local2.y = (((-(_local1.y) + this._stageCenter.y) - ((GameConstants.ROOM_HEIGHT * this.gameViewModel.hospitalViewScale) / 2)) - this.gameView.getVectorDisplayOffset().y);
            this.gameViewModel.gameViewPosition = _local2;
        }
        private function addHiddenBird(_arg1:Event):void{
            this.gameView.addHiddenBird();
        }
        private function removeHiddenBird(_arg1:Event):void{
            this.gameView.removeHiddenBird();
        }

    }
}//package net.wooga.pets.views 
